package com.lin.lostandfound.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.lostandfound.mapper.SystemLogMapper;
import com.lin.lostandfound.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.lin.lostandfound.bean.SystemLog;
import com.lin.lostandfound.service.ISystemLogService;

@Service("systemLogService")
public class SystemLogService implements ISystemLogService {
	@Autowired
	private SystemLogMapper mapper;
	@Autowired
	private StringUtil util;
	// 添加
	@Override
	public boolean add(SystemLog systemLog) {
		
		return mapper.insert(systemLog) != -1;
	}
	
	// 批量删除
	@Override
	public boolean remove(String logId) {
		String[] id = logId.split(",");

		int[] lostsId = new int[id.length];
		for(int i = 0 ;i < id.length; i++){
			lostsId[i] = Integer.parseInt(id[i]);
		}
		int i = mapper.deleteBatchIds(Arrays.asList(lostsId));
		return i != -1;
	}
	
	// 统计所有（分页）
	@Override
	public long queryAllCount(String key) {

		QueryWrapper<SystemLog> wrapper = new QueryWrapper<>();
		if(util.notnull(key)){
			wrapper.like("createBy",key);

		}
		int i = mapper.selectCount(wrapper);
		return i;
	}
	
	// 查询所有
	@Override
	public Map<String, Object> queryAll(String key, Integer pageIndex, Integer pageSize, String sortField, String sortOrder) {
		Page<SystemLog> page_ = new Page<>(pageIndex,pageSize);
		QueryWrapper<SystemLog> wrapper = new QueryWrapper<>();
		if(util.notnull(key)){
			wrapper.like("createBy",key);

		}
		if (util.notnull(sortField)) {
			if(sortOrder.equals("desc")){
				wrapper.orderByDesc(sortField);
			}else{
				wrapper.orderByAsc(sortField);
			}
		}else{
			wrapper.orderByDesc("createDate");
		}
		IPage<SystemLog> page=mapper.selectPage(page_,wrapper);


		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", page.getRecords());
		map.put("total", page.getTotal());

		return map;
	}

}
